function pigrid = startvalgenerator(knotvec,pi_c,scalefactor,upperlinwt,uppercurvfactor,uppertailparams,Display)

if nargin<7
    Display = 0 ;
end

if scalefactor<0 || scalefactor>1
    error('StartValGenerator:InvalidInputs','The variable "scalefactor" must be between 0 and 1...')
end

if upperlinwt<0 || upperlinwt>1
    error('StartValGenerator:InvalidInputs','The variable "upperlinwt" must be between 0 and 1...')
end

if uppercurvfactor<0 || uppercurvfactor>1
    error('StartValGenerator:InvalidInputs','The variable "uppercurvfactor" must be between 0 and 1...')
end
%%%%The following variable will be used to control whether the algorithm puts out groups of 5
%%%%(regular==1) or groups of 
regular = 1 ;


K_c     = length(knotvec) - 1 ;
Gamma   = knotvec([(2:K_c+1)';K_c+1;K_c+1]) - knotvec([1;1;(1:K_c)']) ;
testdom = linspace(knotvec(1),knotvec(end),2000)' ;



%%%%The following line scales the whole function up by 100*scaltefactor percent
pi_scaleup = pi_c.*[1;1;(1+scalefactor)*ones(K_c+1,1)] ;


%%%%The following line scales the whole function down by 100*scaltefactor percent, but it also
%%%%checks to see whether this would violate convexity, since the first two parameters must remain
%%%%fixed.  If so, then it scales the function down by the maximal percentage that would be
%%%%consistent with the first derivative being strictly increasing to within 10^-6.
pi_scaledown = pi_c.*[1;1;(1-scalefactor)*ones(K_c+1,1)] ;

pi3lb = ( -(1/Gamma(1))*pi_scaledown(1) + (1/Gamma(1) + 1/Gamma(2))*pi_scaledown(2) + 10^-6 )*Gamma(2) ;  %%%%This is the minimum value that pi(3) can take on and still be consistent with convexity.
% pi_scaledown(3)
if pi_scaledown(3)<pi3lb 
% % % %     disp('adjusting the scaledown factor to preserve convexity...') 
    adjustfactor = pi3lb/pi_scaledown(3) ;
    pi_scaledown = pi_scaledown.*[1;1;adjustfactor*ones(K_c+1,1)] ; 
end

if regular==1
    pigrid = [pi_c pi_scaledown (pi_scaledown+pi_c)/2 pi_scaleup (pi_c+pi_scaleup)/2] ;
else
    pigrid = [pi_c pi_scaledown (2*pi_scaledown+pi_c)/3 (pi_scaledown+2*pi_c)/3 pi_scaleup (2*pi_c+pi_scaleup)/3 (pi_c+2*pi_scaleup)/3] ;
end

if regular==1
    iterations = 5 ;
else
    iterations = 7 ;
end

for ii=1:iterations
    pi_c = pigrid(:,ii) ;
    %%%%The following loop dampens the upper-tail curvature by re-setting the last uppertailparams parameter values so
    %%%%that the upper tail is a linear continuation of the cost function midway through its support.
    piupper_lin = pi_c ;
    for jj=K_c+1-uppertailparams+1:K_c+1
        piupper_lin(jj+2) = ( (-1/Gamma(jj))*piupper_lin(jj) + (1/Gamma(jj) + 1/Gamma(jj+1))*piupper_lin(jj+1) + 10^-5 )*Gamma(jj+1);
    end
    piupper_lesscurv = pi_c*(1-upperlinwt) + piupper_lin*upperlinwt ;
    pigrid           = [pigrid, piupper_lesscurv, (piupper_lesscurv+pi_c)/2] ;  %#ok
    
    %%%%The following loop amplifies the degree of curvature in the upper tail by successively
    %%%%amplifying the final four parameter values:
    % uppercurvfactor = 1 + uppercurvfactor ;
    piupper_morecurv = pi_c ;
    for jj=K_c+3-uppertailparams+1:K_c+3
        piupper_morecurv(jj:K_c+3) = piupper_morecurv(jj:K_c+3)*(1+uppercurvfactor) ; % = pi.*[ones(K_c-1,1); (1+uppercurvfactor); (1+uppercurvfactor)^2; (1+uppercurvfactor)^3; (1+uppercurvfactor)^4] ;
    end
    pigrid       = [pigrid, piupper_morecurv, (piupper_morecurv+pi_c)/2] ; %#ok
end

for ii=1:iterations
    pi_c = pigrid(:,ii) ;
    %%%%The following loop dampens curvature by re-setting the last K_c+1 parameter values so
    %%%%that the upper tail is a linear continuation of the cost function early in its support.
    pimid_lin = pi_c ;
    for jj=1:K_c+1
        pimid_lin(jj+2) = ( (-1/Gamma(jj))*pimid_lin(jj) + (1/Gamma(jj) + 1/Gamma(jj+1))*pimid_lin(jj+1) + 10^-5 )*Gamma(jj+1);
    end
    pimid_lesscurv = pi_c*(1-upperlinwt) + pimid_lin*upperlinwt ;
    pigrid           = [pigrid, (pimid_lesscurv+pi_c)/2] ;  %#ok
    
    %%%%The following loop amplifies the degree of curvature by successively
    %%%%amplifying the final K_c+1 parameter values:
    % uppercurvfactor = 1 + uppercurvfactor ;
    pimid_morecurv = pi_c ;
    for jj=3:K_c+3
        pimid_morecurv(jj:K_c+3) = pimid_morecurv(jj:K_c+3)*(1+uppercurvfactor/2) ; % = pi.*[ones(K_c-1,1); (1+uppercurvfactor); (1+uppercurvfactor)^2; (1+uppercurvfactor)^3; (1+uppercurvfactor)^4] ;
    end
    pigrid       = [pigrid, (pimid_morecurv+pi_c)/2] ; %#ok
end


if Display==1
    figure(123456)
    hold on
    for ii=1:length(pigrid(1,:))
        Ctemp = BsplineEval3(knotvec,pigrid(:,ii),testdom) ;
        plot(testdom,Ctemp)
    end
end



end